<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>需求池-产品需求列表</title>
        <link href="../css/mems-index.css" type="text/css" rel="stylesheet"/>
        <link rel="stylesheet" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
        <link href="../css/foundation-datepicker.css" rel="stylesheet"/>
    </head>
<body>
<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a href="pm-need-list-mems.html">需求池</a></div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
    </ul>
</aside>


<!--content-->
<div class="content">
    <div class="page-inner" style="padding-top: 13px">
        <ul class="item-tab clear">
            <li><a href="pm-need-list.html">原始需求</a></li>
            <li><a class="current" href="pm-need-list-mems.html">产品需求</a></li>
        </ul>
        <div class="commit-new-item"><a href="pm-need-commite-pm.html" class="btn" style="width: 120px" id="creatpmneed">创建产品需求&nbsp;<b>+</b></a></div>

        <table class="mems-search">
            <tr>
                <td>需求名称：<input  id="name" /></td>
                <td>产品：<select id="productList" onchange="_API.getFunctionList('functionList',false,true,'productList')"></select></td>
                <td>功能模块：<select id="functionList"><option>请选择</option></select></td>
                <td>处理状态：<select id="status"></select></td>
                <!--  -->
                <td align="right"><button class="btn" id="checkout">查询</button></td>
            </tr>
            <tr>
                <td colspan="2">提出时间：<input id="dpd1"/>～<input id="dpd2"/></td>
                <td colspan="2">优先级：<select id="priority"></select></td>
            </tr>
        </table>

        <div class="item-content">
            <div class="table-box">
                <table>
                    <thead>
                        <tr class="odd">
                        <th>ID</th>
                        <th>需求名称</th>
                        <th>产品</th>
                        <th>功能模块</th>
                        <th>优先级</th>
                        <!-- <th>附件</th> -->
                        <th>提出人</th>
                        <th>提出时间</th>
                        <th>处理状态</th>
                        <th>预计工时(小时)</th>
                        <th>关联原始需求数</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody id="bodyList">
                        <!-- <tr>

                            <td>1</td>
                            <td>医信云服1.0</td>
                            <td>合同管理</td>
                            <td><a href="pm-need-detail-pm.html" class="link">医信云服1.0</a></td>
                            <td>中</td>
                            <td>--</td>
                            <td>刘凯</td>
                            <td>2012-09-09</td>
                            <td>方案设计</td>
                            <td>2/6</td>
                        </tr> -->
                    
                    </tbody>
                </table>
            </div>
            <div class="pages clear">
                <div class="page-list clear" id="pageList">
                
                </div>
            </div>
        </div>
    </div>

</div>
<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/foundation-datepicker.min.js"></script>
<script src="../js/common.js"></script>
<script type="text/javascript">

    //权限控制
    var roleid = $.cookie('userrole');
    if(roleid == 1 || roleid == 2 || roleid == 3){
        $('#creatpmneed').remove()
    }
    function getData(p,checPara,sessionPara){
        var PerPage = 20;
        var url  = _ip+'/product/getRequireList?page='+p+'&size='+PerPage;
        var data = {};
        if(checPara){
            var para = {
                product: $('#productList').val() || null,
                feature: $('#functionList').find('option:selected').text() == '请选择'?null:$('#functionList').val(),
                name:$('#name').val() || null,
                level:$('#levelList').val() || null,
                priority:$('#priority').val() || null,
                status:$('#status').val() || null,
                startTime: $('#dpd1').val()?$('#dpd1').val() + ' 00:00:00' : null,
                endTime: $('#dpd2').val()?$('#dpd2').val() + ' 00:00:00' : null
            }
            data = para
        }
        if(sessionPara){
            data = sessionPara;
        }
        $.ajax({
            url:url,
            type : "POST",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: 'json',
            contentType:'application/json',
            data:JSON.stringify(data),
            success: function(json) {
                if(json.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                var storage = {
                    p:p,
                    para:data
                }
                sessionStorage.setItem('pmNeed', JSON.stringify(storage));
                if(!json.data || json.data.length <= 0){
                    $('#bodyList').html('<tr><td colspan="11">暂无数据！</td></tr>');
                    $('#pageList').html('');
                    return false;
                }
                var _data     = [];
                var _pager    = [];
                var totalPage = json.totlePage;
                var prev      = p-1;
                if(prev < 1)prev = 1;
                var next = p + 1;
                if(next > totalPage)next = totalPage;
                for(var k = 0, kk = json.data.length; k < kk; k++){
                    var msg = json.data[k];

                    _data.push('<tr>');
                    _data.push('<td>'+msg.id+'</td>');
                    _data.push('<td><a href="pm-need-detail-pm.html?id='+msg.id+'" class="link" title="'+msg.name+'">'+(msg.name.length<12?msg.name:msg.name.substring(0,12)+'...' )+'</a></td>');

                    _data.push('<td>'+ msg.product +'</td>');
                    _data.push('<td>'+msg.feature+'</td>');
                    _data.push('<td>'+msg.priority+'</td>');
           
                    _data.push('<td>'+msg.creater+'</td>');
                    _data.push('<td>'+msg.createTime+'</td>');
                    _data.push('<td>'+msg.status+'</td>');
                    _data.push('<td>'+(msg.expectWorkhour ? msg.expectWorkhour : '0')+'</td>');
                    _data.push('<td>'+msg.originalCount+'</td>');

                    if(roleid==0 || roleid== 9){
                        _data.push('<td><a class="link" href="javascript:" onclick="deleteList('+msg.id+')">删除</a></td>');

                    }else{
                        _data.push('<td></td>');
                    }
                }
                _pager.push('<span>共：'+ totalPage +'页</span>');
                _pager.push('<span>您正在第：'+p+'页</span>')
                _pager.push('<a href="javascript:;" class="page-pre" onclick="getData('+prev+',true'+');return !1;">上一页</a>');
                _pager.push('<a href="javascript:;" class="page-next" onclick="getData('+next+',true'+');return !1;">下一页</a>');
                _pager.push('<span>前往第</span>');
                _pager.push('<input type="text" id="goToPage"/>');
                _pager.push('<span>页</span>');
                _pager.push('<button id="goToPageBtn">GO</button>');
                $('#bodyList').html(_data.join(""));
                $('#pageList').html(_pager.join(""));
                $('#goToPageBtn').on('click',function () {
                    var v = $('#goToPage').val();
                    if(!/^[1-9]\d*$/.test(v)){
                        return false;
                    }
                    if(v>1 && v<totalPage){
                        getData(v,true);
                    }
                    if(v <= 1){
                        getData(1,true);
                        return false;
                    }
                    if(v >= totalPage){
                        getData(totalPage,true);
                        return false;
                    }
                })
            }
        })
    }

    $('#checkout').on('click',function(){

        getData(1,true);
    })

    function init() {
        var sessionPage = JSON.parse(sessionStorage.getItem('pmNeed')) || null;
        if(sessionPage){
            var p = sessionPage.p;
            var para = sessionPage.para;
            getData(p,false,para);
            $('#name').val(sessionPage.para.name);
            $('#dpd1').val(sessionPage.para.startTime?sessionPage.para.startTime.split(' ')[0]:'');
            $('#dpd2').val(sessionPage.para.endTime?sessionPage.para.endTime.split(' ')[0]:'');
            _API.getProductList('productList',sessionPage.para.product,true)
                    .getLevelList('levelList',sessionPage.para.level,true)
                    .getStatusListPm('status',sessionPage.para.status,true)
                    .getPriorityList('priority',sessionPage.para.priority,true);
        }else{
            getData(1);
            _API.getProductList('productList',0,true)
                    .getLevelList('levelList',0,true)
                    .getStatusListPm('status',0,true)
                    .getPriorityList('priority',0,true);
        }
    }
    init()


    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
    var checkin = $('#dpd1').fdatepicker({
        onRender: function (date) {
            return date.valueOf() < now.valueOf() ? '' : '';
        },
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (ev) {
        if (ev.date.valueOf() > checkout.date.valueOf()) {
            var newDate = new Date(ev.date)
            newDate.setDate(newDate.getDate() + 1);
            checkout.update(newDate);
        }
        checkin.hide();
        $('#dpd2')[0].focus();
    }).data('datepicker');
    var checkout = $('#dpd2').fdatepicker({
        onRender: function (date) {
            return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
        },
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (ev) {
        checkout.hide();
    }).data('datepicker');

    function  deleteList(id) {
        if(confirm("确认删除吗？")){
            var url = _ip + '/product/deleteRequireById?id='+id;
            $.ajax({
                url:url,
                type: 'GET',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                dataType: 'json',
                contentType:'application/json',
                jsonpCallback: 'login',
                success: function(json) {
                    if(!json.success){
                        alert(json.message);
                    }else{
                        getData(1);
                    }
                }
            })
        }

    }

</script>
</body>
</html>